/*
 * Dlinkedlist.c
 *
 *  Created on: 2011-12-1
 *      Author: tracl01
 */

#include <stdlib.h>
#include <stdio.h>

typedef int Elemtype;
typedef struct DuLNode {
	Elemtype data;
	struct DuLNode *prior;
	struct DuLNode *next;
}DuLNode,*DuLinklist;

int insertDuL(DuLinklist L, int i, Elemtype e){
	DuLinklist p = L;
	int j = 0;
	while(p && j<i-1){		//将p定到待插入位置的前趋结点
		p = p->next;
		j++;
	}
	if (!p || j>i-1) {
		return EXIT_FAILURE;
	} else {
		DuLinklist newnode = (DuLinklist)malloc(sizeof(DuLNode));
		if (!newnode) {
			exit(EXIT_FAILURE);
		}
		DuLinklist q = p->next;
		newnode->prior = p;newnode->next = q;
		p->next = newnode; q->prior = newnode;
		newnode->data = e;
		return EXIT_SUCCESS;
	}
}

int deleteDuL(DuLinklist L, int i, Elemtype *e){
	DuLinklist p = L;
	int j = 0;
	while(p->next && j<i-1){
		p = p->next;
		j++;
	}
	if(!(p->next) || j>i-1){
		return EXIT_FAILURE;
	} else {
		DuLinklist q = p->next;
		e = q->data;
		q->next->prior = p; p->next = q->next;
		free(q);
		return EXIT_SUCCESS;
	}
}
